package cn.usian.controller;

import cn.usian.common.R;
import cn.usian.entity.Category;
import cn.usian.entity.DTO;
import cn.usian.entity.Dish;
import cn.usian.entity.DishFlavor;
import cn.usian.service.CategoryService;
import cn.usian.service.DishFlavorService;
import cn.usian.service.DishService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/dish")
public class DishController {
    @Autowired
    private DishService dishService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private DishFlavorService dishFlavorService;

    //查询所有
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name) {
        return dishService.page(page, pageSize, name);
    }

    //删除
    @DeleteMapping
    public R del(long[] ids) {
        dishService.del(ids);
        return R.success("删除成功");
    }

    //修改状态（停售）
    @PostMapping("status/{status}")
    public R status(@PathVariable("status") Integer status, long[] ids) {
        return dishService.status(status, ids);
    }

    //添加菜品
    @PostMapping
    public R add(@RequestBody Dish dish, HttpSession session) {
        dishService.add(dish, session);
        Long id = (long) dishService.selectByName(dish.getName());
        List<DishFlavor> flavors = dish.getFlavors();
        for (int i = 0; i < flavors.size(); i++) {
            DishFlavor dishFlavor = new DishFlavor();
            dishFlavor.setDishId(id);
            dishFlavor.setName(flavors.get(i).getName());
            dishFlavor.setValue(flavors.get(i).getValue());
            dishFlavorService.add(dishFlavor, session);
        }
        return R.success("添加成功");
    }

    //回显菜品
    @GetMapping("{id}")
    public R findById(@PathVariable("id") long id){
        return dishService.findById(id);
    }

    @GetMapping("/list")
    public R<List<Dish>> list(Long categoryId,String name){
        return R.success(dishService.list(categoryId,name));
    }

}
